package de.derhess.projects.pkr.component
{
	import de.derhess.projects.pkr.models.CourtCost;
	
	import mx.events.PropertyChangeEvent;
	
	import spark.components.Button;
	import spark.components.Label;
	import spark.components.supportClasses.SkinnableComponent;
	
	
	/**
	 * Dealing with the court costs
	 * 
	 * l
	 * @author Florian Weil [derhess.de, Deutschland]
	 * @see http://blog.derhess.de
	 * 
	 */
	public class Court extends SkinnableComponent
	{
		//--------------------------------------------------------------------------
		//
		//  Class variables
		//
		//--------------------------------------------------------------------------
		private static const DEFAULT_HEIGHT:Number = 200;
		private static const DEFAULT_WIDTH:Number = 200;
		
		public static const STATE_CONCILIATION_INSTANCE:String = "conciliationInstance";
		public static const STATE_CONCILIATION_APPEAL_ONE:String = "conciliationAppeal1";
		public static const STATE_CONCILIATION_APPEAL_TWO:String = "conciliationAppeal2";
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		
		//--------------------------------------------------------------------------
		//
		//  Initialization
		//
		//--------------------------------------------------------------------------
		[SkinState("conciliationInstance")]
		[SkinState("conciliationAppeal1")]
		[SkinState("conciliationAppeal2")]
		public function Court()
		{
			
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
		}
		
		
		
		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		//----------------------------------
		//  Visual properties
		//----------------------------------
		[SkinPart(required="true")]	public var instanceBtn:Button;
		[SkinPart(required="true")]	public var instanceLabel:Label;
		
		[SkinPart(required="true")]	public var appealOneBtn:Button;
		[SkinPart(required="true")]	public var appealOneLabel:Label;
		
		[SkinPart(required="true")]	public var appealTwoBtn:Button;
		[SkinPart(required="true")]	public var appealTwoLabel:Label;
		
		[SkinPart(required="true")]	public var sumLabel:Label;
		//----------------------------------
		//  Data properties
		//----------------------------------
		private var _dataModel:CourtCost;
		private var dataModelChanged:Boolean = false;
		/**
		 * This is dataModel
		 */
		public function get dataModel():CourtCost
		{
			return _dataModel;
		}
		
		/*
		* @private
		*/
		public function set dataModel(value:CourtCost):void
		{
			if(value && (_dataModel != value)) {
				
				// removeListener from the old data object and add the listener to the new one
				if(_dataModel) {
					this.dataModel.instance1.removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewSum);
					this.dataModel.appeal1.removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewSum);
					this.dataModel.appeal2.removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewSum);
				}
				
				_dataModel = value;
				
				// Add Event Listener maybe in commit Property function for performance
				this.dataModel.instance1.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewSum);
				this.dataModel.appeal1.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewSum);
				this.dataModel.appeal2.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewSum);
				
				dataModelChanged = true;
				invalidateProperties();
			}
		}
		
		private var courtFeeChanged:Boolean = false;
		private var _courtFee:Number;
		/**
		 * This is courtFee
		 */
		public function get courtFee():Number
		{
			return _courtFee;
		}
		
		/*
		* @private
		*/
		public function set courtFee(value:Number):void
		{
			if(_courtFee != value) {
				courtFeeChanged = true;
				_courtFee = value;
				invalidateProperties();
			}
		}
		
		private var instance1FactorChanged:Boolean = false;
		private var _instance1Factor:Number;
		/**
		 * This is instance1Factor
		 */
		public function get instance1Factor():Number
		{
			return _instance1Factor;
		}
		
		/*
		* @private
		*/
		public function set instance1Factor(value:Number):void
		{
			if(_instance1Factor != value) {
				instance1FactorChanged = true;
				_instance1Factor = value;
				invalidateProperties();
			}
		}
		
		private var appeal1FactorChanged:Boolean = false;
		private var _appeal1Factor:Number;
		/**
		 * This is appeal1Factor
		 */
		public function get appeal1Factor():Number
		{
			return _appeal1Factor;
		}
		
		/*
		* @private
		*/
		public function set appeal1Factor(value:Number):void
		{
			if(_appeal1Factor != value) {
				appeal1FactorChanged = true;
				_appeal1Factor = value;
				invalidateProperties();
			}
		}
		
		private var appeal2FactorChanged:Boolean = false;
		private var _appeal2Factor:Number;
		/**
		 * This is appeal2Factor
		 */
		public function get appeal2Factor():Number
		{
			return _appeal2Factor;
		}
		
		/*
		* @private
		*/
		public function set appeal2Factor(value:Number):void
		{
			if(_appeal2Factor != value) {
				appeal2FactorChanged = true;
				_appeal2Factor = value;
				invalidateProperties();
			}
		}
		//--------------------------------------------------------------------------
		//
		//  Additional getters and setters
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		override protected function measure():void
		{
			super.measure();
			measuredMinHeight = measuredHeight = DEFAULT_HEIGHT;
			measuredMinWidth = measuredWidth = DEFAULT_WIDTH;
		}
		
		override protected function commitProperties():void 
		{
			super.commitProperties();
			
			if(courtFeeChanged && dataModel) {
				dataModel.instance1.baseValue = courtFee;
				dataModel.appeal1.baseValue = courtFee;
				dataModel.appeal2.baseValue = courtFee;
				courtFeeChanged = false;
			}
			
			if(instance1FactorChanged && dataModel) {
				dataModel.instance1.factor = instance1Factor;
				instance1FactorChanged = false;
			}
			
			if(appeal1FactorChanged && dataModel) {
				dataModel.appeal1.factor = appeal1Factor;
				appeal1FactorChanged = false;
			}
			
			if(appeal2FactorChanged && dataModel) {
				dataModel.appeal2.factor = appeal2Factor;
				appeal2FactorChanged = false;
			}
		}
		
		override protected function updateDisplayList(unscaledWidth:Number,unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
		}
		
		override protected function getCurrentSkinState():String {
			return currentState;
		}
		
		override protected function partAdded(partName:String,instance:Object):void
		{
			
		}
		
		override protected function partRemoved(partName:String,instance:Object):void
		{
			
		}
		
		
		//----------------------------------
		//  Updating Text Labels
		//----------------------------------
		private function updateLabels():void
		{
			if(dataModel) {
				instanceBtn.label = dataModel.instance1.factor.toString();
				instanceLabel.text = dataModel.instance1.sum.toString();
				
				appealOneBtn.label = dataModel.appeal1.factor.toString();
				appealOneLabel.text = dataModel.appeal1.sum.toString();
				
				appealTwoBtn.label = dataModel.appeal2.factor.toString();
				appealTwoLabel.text =  dataModel.appeal2.sum.toString();
				
				sumLabel.text = dataModel.sum.toString();
			}
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  API
		//
		//--------------------------------------------------------------------------
		
		
		//--------------------------------------------------------------------------
		//
		//  Eventhandling
		//
		//--------------------------------------------------------------------------
		private function handleNewSum(e:PropertyChangeEvent):void
		{
			this.dataModel.sum = dataModel.instance1.sum + dataModel.appeal1.sum + dataModel.appeal2.sum;
			updateLabels();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Broadcasting
		//
		//--------------------------------------------------------------------------
		
		
	}
}
